package com.hb.trade.server.center.au.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.center.au.dto.OrgExtDTO;
import com.hb.core.pojo.center.au.entity.Org;
import com.hb.core.pojo.center.au.entity.OrgExt;
import com.hb.trade.server.center.au.service.OrgExtService;
import com.hb.trade.server.center.au.service.OrgService;
import com.hb.trade.server.center.au.service.RoleService;
import org.fzzn.core.model.enums.LogicEnum;

import java.util.List;

/**
 * 企业模型领域对象
 *
 * @author zhaojk
 */
public class OrgManager extends OrgDTO {


    public static List<OrgDTO> changeOrgDTOList(List<Org> orgList, Boolean disabled) {
        List<OrgDTO> list = CollUtil.list(false);
        if (CollUtil.isEmpty(orgList)) {
            return list;
        }
        for (Org org : orgList) {
            OrgDTO orgDTO = org.to(OrgDTO.class);
            orgDTO.setDisabled(disabled);
            list.add(orgDTO);
        }
        return list;
    }

    /**
     * 验证当前对象中企业名称是否存在
     *
     * @param orgId 排除的企业ID
     * @return
     */
    public String checkName(Long orgId) {
        OrgService orgService = SpringUtil.getBean(OrgService.class);
        QueryWrapper<Org> wrapper = Org.wrapper();
        wrapper.lambda().eq(Org::getOrgName, this.getOrgName());
        wrapper.lambda().ne(orgId != null, Org::getId, orgId);
        long count = orgService.count(wrapper);
        if (count > 0) {
            return "该企业名称已经存在";
        }
        return null;
    }

    /**
     * 初始化分企业的角色信息
     * 该方法只有在新建分公司的时候调用
     *
     * @param roleSign
     */
    public void initRoleInfo(Integer roleSign) {
        Long orgId = getId();
        if (orgId == null) {
            return;
        }
        if (roleSign == null) {
            return;
        }
        // parentId不为0并且不为null, 判断为分公司
        if (getParentId() != 0 && getParentId() != null) {
            RoleService roleService = SpringUtil.getBean(RoleService.class);
            roleService.initData(orgId, LogicEnum.NO.getCode(), 2, roleSign);
        }
    }
    /**
     * 初始化企业信息(复制父级公司的企业信息)
     * 该方法只有在新建分公司的时候调用
     *
     */
    public void initOrgInfo() {
        Long orgId = getId();
        if (orgId == null) {
            return;
        }

        // parentId不为0并且不为null, 判断为分公司
        if ( getParentId() == null || getParentId() == 0) {
            return;
        }
        //复制父级公司的信息
        OrgService orgService = SpringUtil.getBean(OrgService.class);
        QueryWrapper<Org> wrapper = Org.wrapper("industry_id", "province_id", "city_id", "contact_phone", "contact_address", "contact_name", "email", "area_id", "note", "reason", "level", "legal_name", "certificate_number", "certificate_type", "audit_status");
        wrapper.eq("id",getParentId());
        Org parentOrg = orgService.getOne(wrapper);
        if (parentOrg == null){
            return;
        }
        parentOrg.to(this);
        //复制企业扩展信息
        OrgExtService orgExtService = SpringUtil.getBean(OrgExtService.class);
        OrgExt parentOrgExt = orgExtService.getOrgExtByOrgId(getParentId());
        if (parentOrgExt == null){
            return;
        }
        parentOrgExt.setOrgId(null);
        parentOrgExt.setId(null);
        OrgExtDTO orgExt = new OrgExtDTO();
        parentOrgExt.to(orgExt);
        orgExt.buildBaseData(1);
        orgExt.setOrgId(getId());
        setOrgExt(orgExt);
    }
}
